package ezCall;

import javax.microedition.pim.Contact;

import net.rim.blackberry.api.pdap.BlackBerryContact;
import net.rim.device.api.util.CharacterUtilities;


public class Contact_PhoneNum 
{
	private String[] _PhoneNumArray;
	private String[] _PhoneNumNameArray;
	private int CountValue_PhoneNumber;
	private int CountValue_Email;
	private int CountValue;
	
	private boolean _ContactLoaded;
	
	public void Set_Contact( BlackBerryContact c )
	{
		
		_ContactLoaded = false;
		
		CountValue_PhoneNumber =  c.countValues( Contact.TEL );
		CountValue_Email       =  c.countValues( Contact.EMAIL );
		
		CountValue = CountValue_PhoneNumber + CountValue_Email;
		
		if( CountValue > 0 )
		{
			
			_PhoneNumArray     = new String[ CountValue ];
			_PhoneNumNameArray = new String[ CountValue ];
			
			for( int i=0; i<CountValue_PhoneNumber; i++ )
			{
				_PhoneNumArray[i] = c.getString(Contact.TEL, i);
				_PhoneNumNameArray[i] = Get_PhoneNumNameFromField( c.getAttributes( Contact.TEL, i) );
			}
			
			
			for( int i=0; i<CountValue_Email; i++ )
			{
				_PhoneNumArray[i+CountValue_PhoneNumber] = c.getString(Contact.EMAIL, i);
				_PhoneNumNameArray[i+CountValue_PhoneNumber] = "EMAIL";

			}
			
			_ContactLoaded = true;
		}
			
	}
	

	public void Set_ContactEmail( BlackBerryContact c )
	{
		
		_ContactLoaded = false;
		
		CountValue_PhoneNumber =  0;
		CountValue_Email       =  c.countValues( Contact.EMAIL );
		
		CountValue = CountValue_PhoneNumber + CountValue_Email;
		
		if( CountValue > 0 )
		{
			
			_PhoneNumArray     = new String[ CountValue ];
			_PhoneNumNameArray = new String[ CountValue ];
			
			for( int i=0; i<CountValue_PhoneNumber; i++ )
			{
				_PhoneNumArray[i] = c.getString(Contact.TEL, i);
				_PhoneNumNameArray[i] = Get_PhoneNumNameFromField( c.getAttributes( Contact.TEL, i) );
			}
			
			
			for( int i=0; i<CountValue_Email; i++ )
			{
				_PhoneNumArray[i+CountValue_PhoneNumber] = c.getString(Contact.EMAIL, i);
				_PhoneNumNameArray[i+CountValue_PhoneNumber] = "EMAIL";

			}
			
			_ContactLoaded = true;
		}
			
	}	
	
	
	
//CHCBARAM	
	private String Get_PhoneNumNameFromField( int Field  )
	{
		switch( Field )
		{
		case BlackBerryContact.ATTR_FAX:
			return "FAX";
		case BlackBerryContact.ATTR_HOME:
			return "HOME";
		case BlackBerryContact.ATTR_HOME2:
			return "HOME2";			
		case BlackBerryContact. ATTR_MOBILE:
			return "MOBILE";
		case BlackBerryContact.ATTR_PAGER:
			return "PAGER";
		case BlackBerryContact.ATTR_WORK:
			return "WORK";
		case BlackBerryContact.ATTR_WORK2:
			return "WORK2";
		default:
			return "UNKNOWN";
		}
	}

	
	public String Get_PhoneNumNameFromIndex( int Index )
	{
		return _PhoneNumNameArray[Index];
	}
	
	
	public int Get_PhoneNumberCount()
	{
		return CountValue;
	}
	
	
	public boolean isMobileNumber()
	{
		if( Get_MobileNumberIndex() < 0  ) return false;
		else                             return true;
	}
	
	public static boolean isNumber( char Ch )
	{
		return CharacterUtilities.isDigit(Ch);
	}	
	
	public int Get_MobileNumberIndex()
	{
		int ret = -1;
		String  StrBuf;
		
		for( int i=0; i<_PhoneNumArray.length; i++ )
		{
			StrBuf = Remove_SpecialCharFromPhoneNumbner( _PhoneNumArray[i] );			
			
			if( StrBuf.indexOf("010") == 0 ) ret = i;
			else
			if(  StrBuf.indexOf("011") == 0 ) ret = i;
			else
			if(  StrBuf.indexOf("016") == 0 ) ret = i;
			else
			if(  StrBuf.indexOf("017") == 0 ) ret = i;
			else
			if(  StrBuf.indexOf("018") == 0 ) ret = i;
			else
			if(  StrBuf.indexOf("019") == 0 ) ret = i;
			
			if( ret >= 0 ) break;
		}
	
		return ret;
	}
	
	
	
	
	public static int Get_MobileNumberIndex( String[] StrArray )
	{
		int ret = -1;
		String  StrBuf;
		
		for( int i=0; i<StrArray.length; i++ )
		{
			StrBuf = Remove_SpecialCharFromPhoneNumbner( StrArray[i] );			
			
			if( StrBuf.indexOf("010") == 0 ) ret = i;
			else
			if(  StrBuf.indexOf("011") == 0 ) ret = i;
			else
			if(  StrBuf.indexOf("017") == 0 ) ret = i;
			else
			if(  StrBuf.indexOf("018") == 0 ) ret = i;
			else
			if(  StrBuf.indexOf("019") == 0 ) ret = i;
			
			if( ret >= 0 ) break;
		}
	
		return ret;
	}
	
	public static String Remove_SpecialCharFromPhoneNumbner( String PhoneNumber )
	{
		String RetStr = new String();
		
		for( int i=0; i < PhoneNumber.length(); i++  )
		{
			if( isNumber( PhoneNumber.charAt(i) ) )
			{
				RetStr += PhoneNumber.charAt(i);
			}
		}
		
		return RetStr;
	}
	
	public String Get_FirstMobileNumber()
	{
		int MobileNumberIndex;
		
		MobileNumberIndex = Get_MobileNumberIndex();
		
		if( MobileNumberIndex >= 0 ) return _PhoneNumArray[ MobileNumberIndex ];
		else return "";
	}

	
	public String Get_FirstPhoneNumber()
	{
		int MobileNumberIndex;
		
		MobileNumberIndex = Get_MobileNumberIndex();
		
		if( MobileNumberIndex >= 0 ) return _PhoneNumArray[ MobileNumberIndex ];
		else return _PhoneNumArray[0];
	}	
	
	public String[] Get_PhoneNumberArray()
	{
		return _PhoneNumArray;
	}

	public String[] Get_PhoneNumberNameArray()
	{
		return _PhoneNumNameArray;
	}
	
	public boolean Get_ContactLoadedFlag()
	{
		return _ContactLoaded;
	}
}
